<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container">
    <!-- 标签页 -->
    <nav class="tab">
      <a
        @click="changeTab(true)"
        href="javascript:;"
        :class="{ active: hasAccount }"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        @click="changeTab(false)"
        href="javascript:;"
        :class="{ active: !hasAccount }"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <!-- 切换组件 -->
    <div class="tab-content" v-if="hasAccount">
      <!-- 有小兔鲜账户 绑定手机 -->
      <CallbackBind :openId="openId" />
    </div>
    <div class="tab-content" v-else>
      <!-- 没有小兔鲜账户 -->
      <CallbackPatch :openId="openId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import { ref } from 'vue'
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
// 导入绑定手机和完善资料
import CallbackBind from './components/callback-bind.vue'
import CallbackPatch from './components/callback-patch.vue'
// 腾讯
import { userQQLogin } from '@/api/user.js'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import Message from '@/components/Message'
export default {
  name: 'PageCallback',
  components: {
    LoginHeader,
    LoginFooter,
    CallbackBind,
    CallbackPatch
  },
  setup () {
    // 是否有账号
    const hasAccount = ref(false)
    function changeTab (has) {
      hasAccount.value = has
    }
    // 已有小兔鲜账号和qq号已绑定的情况
    const store = useStore()
    const router = useRouter()
    const openId = ref(null)
    if (window.QC.Login.check()) { // 返回true是腾讯服务器登录成功了
      window.QC.Login.getMe(async (openid) => {
        openId.value = openid
        try {
          const res = await userQQLogin(openid)
          // 第一次是失败的,因为没有绑定qq
          console.log(res)
          // 登录成功存储登陆人的qq登录信息
          store.commit('user/updataProfile', res.data.result)
          Message({ type: 'success', text: '登陆成功' })
          router.push('/')
        } catch (error) {
          // console.log(789)
          Message({ type: 'error', text: '没有绑定QQ号' })
        }
      })
    }
    return { hasAccount, changeTab, openId }
  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
